package org.example.myleet.p1331;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class Solution {
    public int[] arrayRankTransform(int[] arr) {
        int[] res = new int[arr.length];
        if (arr.length < 1) {
            return res;
        }
        int[] copy = new int[arr.length];
        System.arraycopy(arr, 0, copy, 0, arr.length);
        Arrays.sort(copy);
        Map<Integer, Integer> numberMapRank = new HashMap<>(arr.length);
        int id = 1, lastNum = copy[0];
        numberMapRank.put(lastNum, id);
        for (int i = 1; i < copy.length; ++i) {
            if (copy[i] > lastNum) {
                ++id;
                numberMapRank.put(copy[i], id);
            }
            lastNum = copy[i];
        }
        for (int i = 0; i < arr.length; ++i) {
            res[i] = numberMapRank.get(arr[i]);
        }
        return res;
    }
}
